package com.tjetc.service.impl;

import com.tjetc.domain.Hospital;
import com.tjetc.mapper.HospitalMapper;
import com.tjetc.mapper.UserMapper;
import com.tjetc.vo.HosClinFan;
import com.tjetc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.domain.FollowHospital;
import com.tjetc.mapper.FollowHospitalMapper;
import com.tjetc.service.FollowHospitalService;

import java.util.List;

@Service
public class FollowHospitalServiceImpl implements FollowHospitalService{

    @Resource
    private FollowHospitalMapper followHospitalMapper;

    @Resource
    private HospitalMapper hospitalMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return followHospitalMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FollowHospital record) {
        return followHospitalMapper.insert(record);
    }

    @Override
    public int insertSelective(FollowHospital record) {
        return followHospitalMapper.insertSelective(record);
    }

    @Override
    public FollowHospital selectByPrimaryKey(Integer id) {
        return followHospitalMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FollowHospital record) {
        return followHospitalMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FollowHospital record) {
        return followHospitalMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Hospital> selNameAndIcon(Integer uid) {
        return followHospitalMapper.selNameAndIcon(uid);
    }

    @Override
    public int deleteByHidAndUid(Integer hid, Integer uid) {
        return followHospitalMapper.deleteByHidAndUid(hid,uid);
    }

    @Override
    public List<Hospital> selByUid(Integer uid) {
        return followHospitalMapper.selByUid(uid);
    }

    @Override
    public Result isFollow(String phone, Integer hid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        FollowHospital followHospital = followHospitalMapper.selectByUidAndHid(uid,hid);
        return Result.ok(followHospital != null);
    }

    @Override
    public Result follow(String phone, Integer hid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        Integer count = followHospitalMapper.selectFollowCountByUid(uid);
        if (count >= 5){
            return Result.fail("关注数量不能超过五个");
        }
        FollowHospital oldFollowHospital = followHospitalMapper.selectByUidAndHid(uid, hid);
        if (oldFollowHospital != null){
            return Result.fail("不能重复关注哦");
        }
        FollowHospital followHospital = FollowHospital.builder().hid(hid).uid(uid).build();
        int rows = followHospitalMapper.insert(followHospital);
        return rows == 1?Result.ok():Result.fail("失败");
    }

    @Override
    public Result cancelFollow(String phone, Integer hid) {
        Integer uid = userMapper.selectByPhone(phone).getId();
        FollowHospital followHospital = followHospitalMapper.selectByUidAndHid(uid, hid);
        if (followHospital == null){
            return Result.fail("失败");
        }
        int rows = followHospitalMapper.deleteByPrimaryKey(followHospital.getId());
        return rows == 1 ? Result.ok() : Result.fail("失败");
    }


    @Override
    public Result getFanCount(String hospitalName) {
        try {
            Hospital hospital = hospitalMapper.selectByHospitalName(hospitalName);
            if (hospital == null) {
                return Result.fail("医院不存在");
            }
            HosClinFan fansCount = followHospitalMapper.getFansCount(hospital.getId());
            return Result.ok(fansCount);
        } catch (Exception e) {
            return Result.fail("获取关注数据失败: " + e.getMessage());
        }
    }
}
